<template>
  <div class="opinion-manage">
    <!-- 上面的查询框 -->
    <div class="query-form">
      <el-form :inline="true" :model="queryForm" ref="form">
        <el-form-item label="意见检索" prop="itemName">
          <el-input
            v-model="queryForm.itemName"
            placeholder="请输入意见标题进行检索"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="action">
        <el-button type="primary" @click="handleCreateOpinion"
          >新增意见</el-button
        >
      </div>
    </div>
    <!-- 下面的表格 -->
    <div class="base-table">
      <el-table :data="itemList" :stripe="true" :border="true">
        <el-table-column label="意见主题" prop="title" />
        <el-table-column label="提交时间" prop="createTime" width="180" />
        <!-- 操作 -->
        <el-table-column label="操作" width="230">
          <template #default="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleCheck(scope.row)"
              >查看</el-button
            >
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDel(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import storage from '../utils/storage'
export default {
  name: 'Opinion',
  data() {
    return {
      itemList: [],
      queryForm: {}
    }
  },
  methods: {
    // 项目检索
    handleQuery() { },
    // 重置查询表单
    handleReset(form) {
      this.$refs[form].resetFields()
    },
    // 新增意见
    handleCreateOpinion() {
      this.$router.push({
        path: `/work/compile/create`
      })
    },
    // 查看意见
    handleCheck(row) {
      this.$router.push({
        path: `/work/compile/${row.id}`
      })
      row.action = 'check'
      storage.setItem('rowInfo', row)
    },
    // 编辑意见
    handleEdit(row) {
      this.$router.push({
        path: `/work/compile/${row.id}`
      })
      row.action = 'edit'
      storage.setItem('rowInfo', row)
    },
    // 删除意见
    handleDel(id) {
      this.$confirm('是否删除该项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$api.opinionOperate({ id, action: 'delete' })
        this.$message.success('删除成功')
        this._getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 请求列表
    async _getList() {
      const res = await this.$api.getOpinionList()
      this.itemList = res
    }
  },
  created() {
    this._getList()
  }
}
</script>

<style lang="scss">
.query-form {
  display: flex;
  justify-content: space-between;
}
</style>